namespace KYO
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Data.SqlClient;
	using System.Text;
	
	
	public class tblUser : Object
	{
		
		#region Static SQL String Memebers
		/// <remarks>This field represents the full SELECT string for the table tblUser, with the WHERE clause.</remarks>
		internal static string _SQL_Select = "SELECT [UserID], [Username], [Password] FROM [dbo].[tblUser] WHERE [UserID]=@UserID ";
		
		/// <remarks>This field represents the full INSERT INTO string for the table tblUser.</remarks>
		internal static string _SQL_Insert = "INSERT INTO [dbo].[tblUser] ([UserID], [Username], [Password]) VALUES([UserID], [Username], [Password]) ";
		
		/// <remarks>This field represents the full UPDATE string for the table tblUser, with the WHERE clause.</remarks>
		internal static string _SQL_Update = "UPDATE [dbo].[tblUser] SET [UserID] = @UserID, [Username] = @Username, [Password] = @Password WHERE [UserID]=@UserID ";
		
		/// <remarks>This field represents the DELETE string for the table tblUser, with the WHERE clause.</remarks>
		internal static string _SQL_Delete = "DELETE FROM [dbo].[tblUser] WHERE [UserID]=@UserID ";
		#endregion
		
		#region Tables Memebers
		/// <remarks>SQL Type:int - </remarks>
		private int _UserID;
		
		[DisplayName("User ID")]
		[Category("Primary Key")]
		public int UserID
		{
			get
			{
				try
				{
					return _UserID;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting UserID", err);
				}
			}
			set
			{
				try
				{
					_UserID = value;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting UserID", err);
				}
			}
		}
		
		/// <remarks>SQL Type:nvarchar - </remarks>
		private string _Username;
		
		[DisplayName("Username")]
		[Category("Column")]
		public string Username
		{
			get
			{
				try
				{
					return _Username;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting Username", err);
				}
			}
			set
			{
				try
				{
					if ((value.Length <= 40))
					{
						_Username = value;
					}
					else
					{
						throw new OverflowException("Error setting Username, Length of value is to long. Maximum Length: 40");
					}
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting Username", err);
				}
			}
		}
		
		/// <remarks>SQL Type:nvarchar - </remarks>
		private string _Password;
		
		[DisplayName("Password")]
		[Category("Column")]
		public string Password
		{
			get
			{
				try
				{
					return _Password;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting Password", err);
				}
			}
			set
			{
				try
				{
					if ((value.Length <= 100))
					{
						_Password = value;
					}
					else
					{
						throw new OverflowException("Error setting Password, Length of value is to long. Maximum Length: 100");
					}
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting Password", err);
				}
			}
		}
		#endregion
		
		#region Public Methods
		public void Select(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblUser._SQL_Select;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				SqlDataReader rs = Com.ExecuteReader();
				while(rs.Read())
				{
					AddFromRecordSet(rs);
				}
				rs.Close();
				Conn.Close();
				rs.Dispose();
				Com.Dispose();
				Conn.Dispose();
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Insert(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblUser._SQL_Insert;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Update(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblUser._SQL_Update;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Delete(string ConnectionString)
		{
			try
			{
				SqlConnection Conn = new SqlConnection(ConnectionString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = tblUser._SQL_Delete;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public string[] GetSqlCommandStrings()
		{
			try
			{
				string[] CommStr = new string[4];
				CommStr[0] = "SELECT [UserID], [Username], [Password] FROM [dbo].[tblUser] WHERE [UserID] = " + _UserID + "";
				CommStr[1] = "INSERT INTO [dbo].[tblUser] ([UserID], [Username], [Password]) VALUES(" + _UserID + ", '" + _Username.Replace("'","''") + "', '" + _Password.Replace("'","''") + "')";
				CommStr[2] = "UPDATE [dbo].[tblUser] SET [Username] = '" + _Username.Replace("'","''") + "' ,[Password] = '" + _Password.Replace("'","''") + "' WHERE [UserID] = " + _UserID + "";
				CommStr[3] = "DELETE FROM [dbo].[tblUser] WHERE [UserID] = " + _UserID + "";
				return CommStr;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		#endregion
		
		#region Private Methods
		private void AddFromRecordSet(SqlDataReader rs)
		{
			try
			{
				// if value from the recordset, to the UserID field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("UserID")) == false))
				{
					UserID = rs.GetInt32(rs.GetOrdinal("UserID"));
				}
				// if value from the recordset, to the Username field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("Username")) == false))
				{
					Username = rs.GetString(rs.GetOrdinal("Username"));
				}
				// if value from the recordset, to the Password field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("Password")) == false))
				{
					Password = rs.GetString(rs.GetOrdinal("Password"));
				}
			}
			catch (SqlException sqlExc)
			{
				throw sqlExc;
			}
			catch (Exception Exc)
			{
				throw Exc;
			}
		}
		
		private SqlParameter[] GetSqlParameters()
		{
			List<SqlParameter> SqlParmColl = new List<SqlParameter>();
			try
			{
				SqlParmColl.Add(SqlUtil.AddSqlParm("@UserID", UserID, SqlDbType.Int));
				SqlParmColl.Add(SqlUtil.AddSqlParm("@Username", Username, SqlDbType.NVarChar));
				SqlParmColl.Add(SqlUtil.AddSqlParm("@Password", Password, SqlDbType.NVarChar));
				return SqlParmColl.ToArray();
			}
			catch (Exception Exc)
			{
				throw Exc;
			}
		}
		#endregion
	}
	
	public class tblUserCollection : System.Collections.CollectionBase
	{
		
		[Description("Adds a new tblUser to the collection.")]
		public int Add(tblUser item)
		{
			int newindex = List.Add(item);
			return newindex;
		}
		
		[Description("Removes a tblUser from the collection.")]
		public void Remove(tblUser item)
		{
			List.Remove(item);
		}
		
		[Description("Inserts an tblUser into the collection at the specified index.")]
		public void Insert(int index, tblUser item)
		{
			List.Insert(index, item);
		}
		
		[Description("Returns the index value of the tblUser class in the collection.")]
		public int IndexOf(tblUser item)
		{
			return List.IndexOf(item);
		}
		
		[Description("Returns true if the tblUser class is present in the collection.")]
		public bool Contains(tblUser item)
		{
			return List.Contains(item);
		}
	}
	
	public enum tblUser_Fields
	{
		
		UserID,
		
		Username,
		
		Password,
	}
}
